package com.tju.shortlink.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import com.tju.shortlink.admin.common.convention.result.Result;
import com.tju.shortlink.admin.common.convention.result.Results;
import com.tju.shortlink.admin.dto.req.UserLoginReqDTO;
import com.tju.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.tju.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.tju.shortlink.admin.dto.resp.UserActualRespDTO;
import com.tju.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.tju.shortlink.admin.dto.resp.UserRespDTO;
import com.tju.shortlink.admin.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/api/short-link/admin/v1/user")
@RequiredArgsConstructor
@Tag(name = "用户相关接口")
public class UserController {

    private final IUserService userService;

    @Operation(summary = "根据用户名获取用户信息")
    @GetMapping("{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username){
        UserRespDTO result = userService.getUserByUsername(username);
        return Results.success(result);
    }

    @Operation(summary = "判断用户名是否存在")
    @GetMapping("has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUsername(username));
    }

    @Operation(summary = "用户注册")
    @PostMapping
    public Result<Void> register(@RequestBody @Validated UserRegisterReqDTO reuestParam){
        userService.Register(reuestParam);
        return Results.success();
    }

    @Operation(summary = "修改用户接口")
    @PutMapping
    public Result<Void> update(@RequestBody @Validated UserUpdateReqDTO requestParam){
        userService.update(requestParam);
        return Results.success();
    }

    @Operation(summary = "用户登录功能")
    @PostMapping("login")
    public Result<UserLoginRespDTO> login(@RequestBody @Validated UserLoginReqDTO requestParam){
        return Results.success(userService.login(requestParam));
    }

    @Operation(summary = "检查用户是否登录")
    @GetMapping("check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token){
        return Results.success(userService.checkLogin(username, token));
    }

    @Operation(summary = "用户退出登录")
    @DeleteMapping("logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token){
        userService.logout(username, token);
        return Results.success();
    }

    @Operation(summary = "根据用户名查询无脱敏用户信息")
    @GetMapping("actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username), UserActualRespDTO.class));
    }

}
